<template>
  <van-list
    v-model="loading"
    :finished="finished"
    finished-text="没有更多了"
    @load="onLoad"
    :error.sync="isError"
    error-text="加载失败，点击重新加载"
    :immediate-check="false"
  >
    <CommentItem
      v-for="(item, index) in list"
      :key="index"
      :comment="item"
      @reply-click="$emit('reply-click', $event)"
    />
  </van-list>
</template>

<script>
import CommentItem from './comment-item.vue'
export default {
  name: 'commentList',
  components: { CommentItem },
  data() {
    return {
      // list: [],
      loading: false,
      finished: false,
      offset: null, // 获取下一页数据的标识
      limit: 10, // 每页数据的条数
      isError: false, // 请求错误的状态标识
      isReplyShow: false,
      currentComment: {} // 当前评论
    }
  },
  props: {
    articleId: {
      type: [String, Number],
      required: true
    },
    list: {
      type: Array,
      default: () => []
    },
    type: {
      type: String,
      default: 'a'
    }
  },
  created() {
    this.onLoad()
  },
  methods: {
    async onLoad() {
      try {
        const res = await this.$api.getComments({
          type: this.type, // 获取评论或者回复 'a'--评论  'c'--回复
          source: this.articleId, // 源id,文章id或评论id
          offset: this.offset, // 偏移
          limit: this.limit // 条数
        })
        const { results } = res.data.data
        this.$emit('onload-succuess', res.data.data)
        this.list.push(...results)
        this.loading = false
        if (results.length !== 0) {
          this.offset = res.data.data.last_id
        } else {
          this.finished = true
        }
      } catch (err) {
        console.log(err)
        this.loading = false
        this.isError = true
      }
    }
  }
}
</script>

<style></style>
